package org.abratuhi.kvngroupware.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import org.abratuhi.kvngroupware.client.KVNGroupwareClient;
import org.abratuhi.kvngroupware.gui.GUIRegistration;
import org.abratuhi.kvngroupware.model.Team;
import org.abratuhi.kvngroupware.model.User;
import org.abratuhi.kvngroupware.network.Message;
import org.jdom.Element;

public class RegistrationController implements ActionListener, WindowListener{
	
	KVNGroupwareClient kvnclient;
	GUIRegistration guiregistration;
	
	public RegistrationController(KVNGroupwareClient kvnclient, GUIRegistration guiregistration){
		this.kvnclient = kvnclient;
		this.guiregistration = guiregistration;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource().equals(guiregistration.buttonCreateUser)){
			String[] newUserData = guiregistration.getNewUserData();
			User u = new User(newUserData[0], newUserData[1], newUserData[2], newUserData[3], newUserData[4]);
			
			String[] attrNames = new String[]{"type", "action"};
			String[] attrValues= new String[]{"application", "create_user"};
			String text = new String();
			Element[] content = new Element[]{u.toElement()};
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
		else if(e.getSource().equals(guiregistration.buttonCreateTeam)){
			String[] newTeamData = guiregistration.getNewTeamData();
			Team t = new Team(newTeamData[0], newTeamData[1], newTeamData[2]);
			
			String[] attrNames = new String[]{"type", "action"};
			String[] attrValues= new String[]{"application", "create_team"};
			String text = new String();
			Element[] content = new Element[]{t.toElement()};
			
			Message message = kvnclient.createMessage(attrNames, attrValues, text, content);
			kvnclient.sendMessage(message);
		}
	}

	@Override
	public void windowActivated(WindowEvent e) {}

	@Override
	public void windowClosed(WindowEvent e) {}
	
	@Override
	public void windowClosing(WindowEvent e) {
		guiregistration.disposeJFrame();
	}

	@Override
	public void windowDeactivated(WindowEvent e) {}

	@Override
	public void windowDeiconified(WindowEvent e) {}

	@Override
	public void windowIconified(WindowEvent e) {}

	@Override
	public void windowOpened(WindowEvent e) {}

}
